<?php
	$host = '0.0.0.0';
	$port = 1238;
	$ws = new swoole_websocket_server($host, $port);

	/**
	 * $ws 服务器
	 * $req 客户端
	 */
	$ws->on('open', function($ws, $req){
		$ws->push($req->fd, "connect is success！");	
	});

	$ws->on('message', function($ws, $req){
		//加装定时器
		$data = json_decode($req->data, true);
		if($data['message'] == 'start') {
			swoole_timer_tick(5000, function($timer_id) use ($ws, $req) {
				$res = [
					'type' => 'timer',
					'message' => 'timer is start',
					'timer_id' => $timer_id
				];
				$ws->push($req->fd, json_encode($res));
			});
		} else if ($data['message'] == 'stop') {
			swoole_timer_clear($data['timer_id']);
			$ws->push($req->fd, $req->data);
		} else {
			$ws->push($req->fd, $req->data);
		}
	});

	$ws->on("close", function($ws, $req){
		echo "close \n";
	});

	$ws->start();
